package com.spdzen.service.impl;

import com.spdzen.model.Video;
import com.github.pagehelper.PageHelper;
import com.spdzen.mapper.VideoMapper;
import com.spdzen.service.VideoService;
import com.spdzen.support.PageNumParam;
import com.spdzen.support.PageOffsetParam;
import com.spdzen.support.PageUtils;
import com.spdzen.support.SimplePage;
import com.spdzen.support.SimpleServiceImpl;
import com.spdzen.support.SimpleStringUtils;
import com.spdzen.vo.VideoAdminVO;
import com.spdzen.vo.search.VideoAdminSearch;

import java.util.List;

import org.springframework.stereotype.Service;

/**
 * 视频  服务实现类
 */
@Service("videoService")
public class VideoServiceImpl extends SimpleServiceImpl<VideoMapper, Video> implements VideoService {
	@Override
	public SimplePage<VideoAdminVO> selectPage(PageNumParam param, VideoAdminSearch search) {
		String title = search.getTitle();
		search.setTitle(SimpleStringUtils.likeSqlString(title));
		
		int pageNum = PageUtils.checkPageNum(param.getPageNum());
		int pageSize = PageUtils.checkPageSize(param.getPageSize());
		
		PageHelper.startPage(pageNum, pageSize);
		
		List<VideoAdminVO> rows = baseMapper.selectAdminVO(search);
		
		return PageUtils.toSimplePage(rows);
	}
	
	@Override
	public SimplePage<VideoAdminVO> selectPage(PageOffsetParam param, VideoAdminSearch search) {
		String title = search.getTitle();
		search.setTitle(SimpleStringUtils.likeSqlString(title));
		
		int offset = PageUtils.checkOffset(param.getOffset());
		int limit = PageUtils.checkLimit(param.getLimit());
		
		PageHelper.offsetPage(offset, limit);
		
		List<VideoAdminVO> rows = baseMapper.selectAdminVO(search);
		
		return PageUtils.toSimplePage(rows);
	}
	
	@Override
	public VideoAdminVO selectAdminVOById(Long id) {
		VideoAdminSearch search = new VideoAdminSearch();
		search.setId(id);
		
		List<VideoAdminVO> rows = baseMapper.selectAdminVO(search);
		
		if(rows == null || rows.size() == 0) {
			return null;
		}else{
			return rows.get(0);
		}
	}
}
